package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.user;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.Updatable;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.UserInfoPanel;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.util.Refresher;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.util.RefresherKiller;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.portal.Portlet;

/**
 * @author Renato Miceli
 */
public class UserInfoPortlet extends Portlet implements Updatable<UserInfo> {

	private final UserInfoPanel userInfoPanel;

	private final InfoManager<UserInfo> manager;

	public UserInfoPortlet(TabPanel centerPanel, InfoManager<UserInfo> manager, InfoManager<UserInfo> loggedManager) {

		this.setTitle("User Info");
		this.setLayout(new ColumnLayout());

		this.userInfoPanel = new UserInfoPanel();
		this.manager = manager;
		
		this.add(userInfoPanel);
		this.add(new UserInfoPortletToolbar(centerPanel, this, manager, loggedManager));

		Refresher<UserInfoPortlet> timer = new Refresher<UserInfoPortlet>(this) {

			@Override
			public void run() {
				this.getRefreshableObject().updateInfo();
			}

		};
		timer.defaultSchedule();
		this.addListener(new RefresherKiller(timer));

		updateInfo();
	}

	public void updateInfo() {
		updateInfo(manager.getInfo());
	}

	/**
	 * {@inheritDoc}
	 */
	public void updateInfo(UserInfo currentInfo) {
		userInfoPanel.updateInfo(currentInfo);
	}

}
